const { Controller } = require('egg')
const dayjs = require('dayjs')
const town = require('../../data/town')
const village = require('../../data/village')

class ApiController extends Controller {
  /**
   * @api {get} /town/scan [乡镇] 扫码数据
   * @apiGroup town
   * 
   * @apiDescription 场所码管理-各多镇扫码人次分布
   * 
   * @apiParam {String} [scope=day] 范围 day（天）、week（周）、month（月）
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": [
          {
              "total": 34,
              "label": "扶沟县"
          }
      ]
  }
   */
  async scan() {
    const { ctx } = this
    const { query } = ctx
    const scope = query.scope || 'day'

    const returnData = await this.service.trackData.getData(`${scope}_town_scan`)

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  /**
   * @api {get} /town/place-code [乡镇] 场所码数量
   * @apiGroup town
   * @apiDescription 场所码管理-各乡镇累计领取场所码数星
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": [
          {
              "value": 22,
              "label": "桐丘街道"
          },
          {
              "value": 13,
              "label": "扶亭街道"
          }
      ]
  }
   */
  async placeCode() {
    const { ctx } = this

    const returnData = await this.service.trackData.getData(`town_place_code`)

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  /**
   * @api {get} /town/person-enter [乡镇] 人员流入
   * @apiGroup town
   * @apiDescription 返乡报备-各乡镇返乡情况
   * 
   * @apiParam {String} [scope=day] 范围 day（天）、week（周）、month（月）
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": [
          {
              "value": 23, // 返乡人事
              "label": "练寺镇", // 乡镇名称
              "percent": 65.71 // 占比
          }
      ]
  }
   */
  async personEnter() {
    const { ctx } = this
    const { query } = ctx
    const scope = query.scope || 'day'

    const returnData = await this.service.trackData.getData(`${scope}_town_person_enter`)

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  /**
   * @api {get} /town/village [乡镇] 村数量
   * @apiGroup town
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": [
          {
            "label": "黄桥乡",
            "value": 0
          },
      ]
  }
   */
  async village() {
    const { ctx } = this
    const { query } = ctx

    const returnData = town.map(townName => {
      const villageList = village.filter(item => item.townName == townName)

      return {
        label: townName,
        value: villageList.length,
      }
    })

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }

  /**
   * @api {get} /town/hs-collect [乡镇] 核酸采集
   * @apiGroup town
   * 
   * @apiSuccessExample {json} Success-Response:
  {
      "code": 0,
      "msg": "success",
      "data": [
          {
              "total": 34,
              "label": "扶沟县"
          }
      ]
  }
   */
  async hsCollect() {
    const { ctx } = this

    const returnData = await this.service.trackData.getData('hs_collect')

    console.log('returnData', returnData)

    ctx.body = {
      code: 0,
      msg: 'success',
      data: returnData,
    }
  }
}

module.exports = ApiController
